import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _pick from "lodash/pick";
import _forEach from "lodash/forEach";
import _startsWith from "lodash/startsWith";
import _get from "lodash/get";
import _isString from "lodash/isString";
import _omit from "lodash/omit";

/* eslint-disable react/require-default-props */
import React from 'react';
import { Spin } from 'antd';
import DynamicToolbar from "./DynamicToolbar";
import DynamicForm from "./DynamicForm";
import DynamicSearchForm from "./DynamicSearchForm";
import DynamicTable from "./DynamicTable";
import DynamicModal from "./DynamicModal";
import { dealObjectProps, set } from "./utils";
import { dynamicComponentOmitProps, dynamicComponentOmitConfigProps, contextPrefix, contextOmitDynamicTableProps } from "./config";
import DynamicTabs from "./DynamicTabs";
set('DynamicToolbar', DynamicToolbar);
set('DynamicForm', DynamicForm);
set('DynamicSearchForm', DynamicSearchForm);
set('DynamicTable', DynamicTable);
set('DynamicModal', DynamicModal);
set('DynamicTabs', DynamicTabs);

var DynamicComponent = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicComponent, _React$Component);

  var _super = _createSuper(DynamicComponent);

  function DynamicComponent() {
    _classCallCheck(this, DynamicComponent);

    return _super.apply(this, arguments);
  }

  _createClass(DynamicComponent, [{
    key: "dealCommonContainerProps",
    value: function dealCommonContainerProps() {
      var _this$props = this.props,
          context = _this$props.context,
          _this$props$template = _this$props.template,
          template = _this$props$template === void 0 ? {} : _this$props$template;

      var otherProps = _omit(this.props, dynamicComponentOmitProps);

      var configProps = dealObjectProps(_omit(template, dynamicComponentOmitConfigProps));
      var templateProps = template.props;

      switch (template.templateType) {
        case 'DynamicTable':
          templateProps = _omit(template.props, contextOmitDynamicTableProps);
          break;
        // form 和 toolbar 还没有要在运行时处理的属性

        case 'DynamicForm':
        case 'DynamicToolbar':
        default:
          break;
      }

      var dealProps = dealObjectProps(templateProps, context);
      return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, configProps), otherProps), dealProps), {}, {
        context: context
      });
    }
  }, {
    key: "getDynamicComponent",
    value: function getDynamicComponent() {
      var _this$props2 = this.props,
          context = _this$props2.context,
          _this$props2$template = _this$props2.template,
          template = _this$props2$template === void 0 ? {} : _this$props2$template;
      var dynamicComponent = null;
      var props = {}; // DynamicModal

      if (template.templateType === 'DynamicModal') {
        var otherProps = _omit(this.props, dynamicComponentOmitProps);

        var configProps = _omit(template, dynamicComponentOmitConfigProps); // 处理 Modal 的 afterSave 属性, afterHide, afterShow


        _forEach(template.props, function (v, k) {
          switch (k) {
            case 'afterSave':
            case 'afterHide':
            case 'afterShow':
              if (template.props[k]) {
                if (_isString(template.props[k]) && _startsWith(template.props[k], contextPrefix)) {
                  var attributePath = template.props[k].substr(5);
                  Object.defineProperty(props, k, {
                    get: function get() {
                      return _get(context, attributePath);
                    },
                    enumerable: true
                  });
                } else {
                  props[k] = v;
                }
              }

              break;

            default:
              props[k] = v;
          }
        });

        _forEach(otherProps, function (v, k) {
          props[k] = v;
        });

        _forEach(configProps, function (v, k) {
          props[k] = v;
        });
      } else {
        props = this.dealCommonContainerProps();
      } // DynamicModal


      switch (template.templateType) {
        case 'DynamicForm':
          dynamicComponent = React.createElement(DynamicForm, props);
          break;

        case 'DynamicToolbar':
          dynamicComponent = React.createElement(DynamicToolbar, props);
          break;

        case 'DynamicSearchForm':
          dynamicComponent = React.createElement(DynamicSearchForm, props);
          break;

        case 'DynamicTable':
          dynamicComponent = React.createElement(DynamicTable, _objectSpread(_objectSpread({}, props), _pick(template.props, contextOmitDynamicTableProps)));
          break;

        case 'DynamicModal':
          dynamicComponent = React.createElement(DynamicModal, props);
          break;

        case 'DynamicTabs':
          dynamicComponent = React.createElement(DynamicTabs, props);
          break;

        default:
          dynamicComponent = null;
          break;
      }

      return dynamicComponent;
    }
  }, {
    key: "render",
    value: function render() {
      var dynamicComponent = this.getDynamicComponent();
      return /*#__PURE__*/React.createElement(Spin, {
        spinning: dynamicComponent === null
      }, dynamicComponent);
    }
  }]);

  return DynamicComponent;
}(React.Component);

DynamicComponent.defaultProps = {};
export { DynamicComponent as default };